import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

public class IsYesterday {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // Date testDate = sdf.parse("2024-12-11 00:00:00");
        Date testDate = sdf.parse("2024-12-16 13:59:59");

        // 调用 isNewData 方法
        boolean result = isNewData(testDate);

        // 输出结果
        System.out.println("测试日期是否是昨天的数据: " + result);
    }

    public void isYesterday(Date date) {
        // 假设你要判断的时间是date

        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取昨天的日期
        LocalDate yesterday = today.minusDays(1);

        // 将Date转换为Instant，再转换为LocalDate
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDate targetDate = instant.atZone(zone).toLocalDate();

        // 比较日期
        if (targetDate.isEqual(today)) {
            System.out.println("这个时间是今天的");
        } else if (targetDate.isEqual(yesterday)) {
            System.out.println("这个时间是昨天的");
        } else {
            System.out.println("这个时间既不是今天也不是昨天");
        }
    }

    public static Boolean isNewData(Date createDate) {
        if (createDate == null)
            return null;

        // 获取今天的开始时间
        LocalDate today = LocalDate.now();
        ZonedDateTime todayStart = today.atStartOfDay(ZoneId.systemDefault());
        Date todayStartDate = Date.from(todayStart.toInstant());

        // 获取昨天的开始时间
        LocalDate yesterday = today.minusDays(1);
        ZonedDateTime yesterdayStart = yesterday.atStartOfDay(ZoneId.systemDefault());
        Date yesterdayStartDate = Date.from(yesterdayStart.toInstant());
        System.out.println("昨天的开始时间：" + yesterdayStartDate);
        System.out.println("今天的开始时间：" + todayStartDate);
        System.out.println(createDate.after(yesterdayStartDate));
        System.out.println(createDate.before(todayStartDate));
        // 判断 createDate 是否在昨天开始时间到今天开始时间之间
        return createDate.after(yesterdayStartDate) && createDate.before(todayStartDate);
    }
}
